// pages/demo6/demo6.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		question: [{
			content: [{
				type: "radio"
			}],
			option:[{}]
		}],
		current: 0
	},
	contentInput: function(e){
		var question = this.data.question;
		var current = this.data.current;
		question[current].content.title = e.detail.value;
		this.setData({
			question
		});
		console.log(this.data.question);
	},
	getImages: function(e){
		var question = this.data.question;
		var current = this.data.current;
		var index = e.currentTarget.dataset.index;
		question[current][e.detail.type][index].images = e.detail.images;
		this.setData({
			question
		});
		console.log(this.data.question);
	},
	getText: function(e){
		var question = this.data.question;
		var current = this.data.current;
		var index = e.currentTarget.dataset.index;
		question[current][e.detail.type][index].explain = e.detail.text;
		this.setData({
			question
		});
		console.log(this.data.question);
	},
	isChecked: function(e){
		var question = this.data.question;
		var current = this.data.current;
		var isCheckbox = e.detail;
		if (isCheckbox) {
			question[current].content.type = "checkbox";
		}else{
			question[current].content.type = "radio";
		}
		this.setData({
			question
		});
		console.log(this.data.question);
	},
	del: function(e){
		var question = this.data.question;
		var current = this.data.current;
		var index = e.currentTarget.dataset.index;
		if (question[current]["option"].length > 1){
			question[current]["option"].splice(index, 1);
			this.setData({
				question
			})
		}else{
			wx.showToast({
				title: '至少含有一项',
				icon: 'none',
				duration: 2000
			})
		}
		console.log(this.data.question);
	},
	submit: function(e){
		var question = this.data.question;
		wx.navigateTo({
			url: './generate/index?question=' + JSON.stringify(question)
		})
	},
	addOption: function(e){
		var question = this.data.question;
		var current = this.data.current;
		question[current]["option"].push({});
		this.setData({
			question
		});
		console.log(this.data.question);
	},
	up: function (e) {
		var question = this.data.question;
		var current = this.data.current;
		var index = e.currentTarget.dataset.index;
		if(index > 0){
			var temp = question[current].option.splice(index, 1)[0];
			question[current].option.splice(index - 1, 0, temp);
		}else{
			wx.showToast({
				title: '首项不能上移',
				icon: 'none',
				duration: 2000
			});
		}
		this.setData({
			question
		})
		console.log(this.data.question);
	},
	down: function (e) {
		var question = this.data.question;
		var current = this.data.current;
		var index = e.currentTarget.dataset.index;
		if (index < question[current].option.length - 1) {
			var temp = question[current].option.splice(index, 1)[0];
			question[current].option.splice(index + 1, 0, temp);
		} else {
			wx.showToast({
				title: '尾项不能下移',
				icon: 'none',
				duration: 2000
			});
		}
		this.setData({
			question
		})
		console.log(this.data.question);
	},
	inputOption: function(e){
		var question = this.data.question;
		var current = this.data.current;
		var index = e.currentTarget.dataset.index;
		question[current]["option"][index].content = e.detail.value;
		this.setData({
			question
		});
		console.log(this.data.question);
	},
	deleteImage: function(e){
		var question = this.data.question;
		var current = this.data.current;
		//第几项
		var optindex = e.currentTarget.dataset.index;
		//第几张图
		var imgindex = e.detail.index;
		console.log(e.detail)
		question[current][e.detail.type][optindex].images.splice(imgindex, 1);
		this.setData({
			question
		});
	},
	addtp: function(e){
		var question = this.data.question;
		var current = question.length;
		question.push({
			content: [{
				type: "radio"
			}],
			option: [{}]
		});
		this.setData({
			question,
			current
		})
		console.log(this.data.question);
	},
	prev: function(e){
		var question = this.data.question;
		var current = this.data.current;
		if(current > 0){
			current--;
			this.setData({
				current
			})
		}else{
			wx.showToast({
				title: '已经是首项了！',
				icon: "none",
				duration: 2000
			});
		}
	},
	next: function(e){
		var question = this.data.question;
		var current = this.data.current;
		if (current < question.length - 1) {
			current++;
			this.setData({
				current
			})
		} else {
			wx.showToast({
				title: '已经是最后一项了！',
				icon: "none",
				duration: 2000
			});
		}
	}
})